<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/4/6
 * Time: 13:51
 */

namespace services\models;

use Yii;


class Comments extends \common\models\Comments
{


    /*保存评论详情到redis*/
    public function saveCommentToRedis($bestry_uid, $commentsArr, $relationArr){

        //$commentsArr = $this->find()->where(['comment_id' => $comment_id])->asArray()->one();


        if(!empty($commentsArr)) {
            \Yii::$app->redis_comments->zadd(
                'user_comment_' . $bestry_uid,
                $commentsArr['create_time'],
                $commentsArr['comment_id']
            );

            // 设置评论内容
            \Yii::$app->redis_comments->set('comment_content_' . $commentsArr['comment_id'], $commentsArr['content']);
            unset($commentsArr['content']);
            // 设置非content字段的内容
            \Yii::$app->redis_comments->hmset('comment_' . $commentsArr['comment_id'], $commentsArr);

            Yii::$app->redis_comments->incr('u_comment_num_' . $bestry_uid);//个人中心订单评论+1

        }

        if(!empty($relationArr)){

            Yii::$app->redis_comments->hmset('comment_' . $relationArr['comment_id'], $relationArr);
            //加入商品评论列表
            Yii::$app->redis_comments->zadd(
                'goods_comment_' . $relationArr['goods_id'],
                $relationArr['status'].$relationArr['create_time'],
                $relationArr['comment_id']
                );

            //加入好评表
            if(in_array($relationArr['level'], array(4,5))){
                Yii::$app->redis_comments->zadd(
                    'goods_comment_A_' . $relationArr['goods_id'],
                    $relationArr['status'].$relationArr['create_time'],
                    $relationArr['comment_id']
                );

                Yii::$app->redis_comments->incr('fb_num_A_' . $relationArr['goods_id']);//好评数+1


            }
            elseif(in_array($relationArr['level'],array(2,3))){//加入中评表
                Yii::$app->redis_comments->zadd(
                    'goods_comment_B_' . $relationArr['goods_id'],
                    $relationArr['status'].$relationArr['create_time'],
                    $relationArr['comment_id']
                );

                Yii::$app->redis_comments->incr('fb_num_B_' . $relationArr['goods_id']);//中评数+1

            }
            elseif(in_array($relationArr['level'],array(0,1))){//加入差评表
                Yii::$app->redis_comments->zadd(
                    'goods_comment_C_' . $relationArr['goods_id'],
                    $relationArr['status'].$relationArr['create_time'],
                    $relationArr['comment_id']
                );

                Yii::$app->redis_comments->incr('fb_num_C_' . $relationArr['goods_id']);//差评数+1

            }

            Yii::$app->redis_comments->incr('fb_num_' . $relationArr['goods_id']);//评论总数+1

            $num['a'] = Yii::$app->redis_comments->get('fb_num_A_' . $relationArr['goods_id']);
            $num['b'] = Yii::$app->redis_comments->get('fb_num_B_' . $relationArr['goods_id']);
            $num['c'] = Yii::$app->redis_comments->get('fb_num_C_' . $relationArr['goods_id']);

            $total = $num['a']+$num['b']+$num['c'];
            $rate['a'] = round(intval($num['a'])/intval($total) * 100);
            $rate['b'] = round(intval($num['b'])/intval($total) * 100);
            $rate['c'] = 100 - $rate['a'] - $rate['b'];

            //Yii::$app->redis_comments->hmset('feedback_Rate_' . $relationArr['goods_id'], $rate);//好中差评率
            Yii::$app->redis_comments->set('fb_rate_A_' . $relationArr['goods_id'], $rate['a']);//好评率
            Yii::$app->redis_comments->set('fb_rate_B_' . $relationArr['goods_id'], $rate['b']);//中评率
            Yii::$app->redis_comments->set('fb_rate_C_' . $relationArr['goods_id'], $rate['c']);//差评率


        }
        //var_dump(Yii::$app->redis_comments->hgetall('comment_' . $comment_id));
    }

    //评论加试豆+100
    public function addMemberScore($bestry_uid){
        $lkt_uid = MemberInfoBestry::getMemberIdByBestryUid($bestry_uid);

        $scoreModel = new MemberScore();
        $result = $scoreModel->addScore(100, '发表评论', $lkt_uid, 5);
        //添加刷新用户试豆的redis
        return $result;
    }

}